package SwingView.Assignment;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import JDBC.InstructorAssignmentActivities;
import SwingController.ALDownloadFile;
import SwingController.CmsPresenter;
import SwingModel.CmsAssignmentSubmission;
import SwingModel.CmsStudent;

import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JSpinner;

import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.border.EmptyBorder;

public class StudentSubmissionPanel extends JPanel{

	private CmsStudent student;
	private CmsAssignmentSubmission asgmSub;
	private JSpinner jsGrade;

	public StudentSubmissionPanel(CmsStudent student, CmsAssignmentSubmission asgmSub){
		
		this.student = student;
		this.asgmSub = asgmSub;
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		setLayout(gridBagLayout);
		setOpaque(true);
		setBorder(BorderFactory.createLineBorder(Color.WHITE));
		
		JLabel lblStudent = new JLabel("Student: ");
		lblStudent.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		GridBagConstraints gbc_lblStudent = new GridBagConstraints();
		gbc_lblStudent.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_lblStudent.insets = new Insets(10, 15, 5, 5);
		gbc_lblStudent.gridx = 0;
		gbc_lblStudent.gridy = 0;
		add(lblStudent, gbc_lblStudent);
		
		JLabel lblName = new JLabel(student.getFirstName() + " " + student.getLastName());
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.weightx = 0.5;
		gbc_lblName.anchor = GridBagConstraints.LINE_START;
		gbc_lblName.insets = new Insets(10, 0, 5, 5);
		gbc_lblName.gridx = 1;
		gbc_lblName.gridy = 0;
		add(lblName, gbc_lblName);
		
		JLabel lblGivenPoint = new JLabel("Given Point: ");
		lblGivenPoint.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		GridBagConstraints gbc_lblGivenPoint = new GridBagConstraints();
		gbc_lblGivenPoint.insets = new Insets(10, 0, 5, 5);
		gbc_lblGivenPoint.gridx = 2;
		gbc_lblGivenPoint.gridy = 0;
		add(lblGivenPoint, gbc_lblGivenPoint);
		
		SpinnerModel spinnerModel = new SpinnerNumberModel(0,0,150,1);
		jsGrade = new JSpinner(spinnerModel);
		jsGrade.setValue((int)asgmSub.getGivenPoint());
		GridBagConstraints gbc_spinner = new GridBagConstraints();
		gbc_spinner.anchor = GridBagConstraints.LINE_START;
		gbc_spinner.weightx = 0.3;
		gbc_spinner.insets = new Insets(0, 0, 5, 0);
		gbc_spinner.gridx = 3;
		gbc_spinner.gridy = 0;
		add(jsGrade, gbc_spinner);
		
		JPanel jpStatus = new JPanel();
		jpStatus.setOpaque(false);
		FlowLayout fl_jpStatus = (FlowLayout) jpStatus.getLayout();
		fl_jpStatus.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_jpStatus = new GridBagConstraints();
		gbc_jpStatus.gridwidth = 4;
		gbc_jpStatus.insets = new Insets(0, 10, 10, 10);
		gbc_jpStatus.fill = GridBagConstraints.BOTH;
		gbc_jpStatus.gridx = 0;
		gbc_jpStatus.gridy = 1;
		add(jpStatus, gbc_jpStatus);
		
		final JPanel jpSubFiles = new JPanel();
		jpSubFiles.setBorder(new EmptyBorder(10, 30, 10, 30));
		GridBagConstraints gbc_jpSubFiles = new GridBagConstraints();
		gbc_jpSubFiles.weighty = 1.0;
		gbc_jpSubFiles.gridwidth = 4;
		gbc_jpSubFiles.fill = GridBagConstraints.BOTH;
		gbc_jpSubFiles.gridx = 0;
		gbc_jpSubFiles.gridy = 2;
		add(jpSubFiles, gbc_jpSubFiles);
		jpSubFiles.setLayout(new BoxLayout(jpSubFiles, BoxLayout.PAGE_AXIS));
		jpSubFiles.setVisible(false); //not visible by default
		jpSubFiles.setOpaque(true);
		jpSubFiles.setBackground(new Color(220,251,217));
		
		//Instantiate the fields
		if(asgmSub.getSubFiles().size() <= 0){
			setBackground(new Color(255,195,182));//Red background
			
			JLabel lblNoSubmission = new JLabel("No Submission");
			lblNoSubmission.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			
			jpStatus.add(lblNoSubmission);
		}
		else{
			setBackground(new Color(178,240,195));//green background
			
			JLabel lblSubmission = new JLabel("Submitted On: ");
			lblSubmission.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			jpStatus.add(lblSubmission);
			
			JLabel lblDate = new JLabel(CmsPresenter.sdf.format(asgmSub.getCreatedDate())+ "          ");
			lblDate.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
			jpStatus.add(lblDate);
			
			final JButton jbtViewSub = new JButton("View Submission");
			jbtViewSub.setForeground(Color.BLUE);
			jbtViewSub.setOpaque(false);
			jbtViewSub.setBorder(BorderFactory.createEmptyBorder());
			jbtViewSub.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jbtViewSub.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					if(jbtViewSub.getText().equals("View Submission")){
						jpSubFiles.setVisible(true);
						jbtViewSub.setText("Hide Submission");
					}
					else{
						jbtViewSub.setText("View Submission");
						jpSubFiles.setVisible(false);
					}
				}
			});
			jpStatus.add(jbtViewSub);
			
			for(File file : asgmSub.getSubFiles()){
				jpSubFiles.add(new FileButton(file));
			}
		}
	}
	
	public void submitGradeUpdate(){
		System.out.println("UPDATE GRADE FOR THIS STUDENT:\n" 
							+ "- asgmID - " + asgmSub.getAsgmID()
							+ "- courseID - " + CmsPresenter.user.getSelectedCourse().getCourseID()
							+ "- username - " + student.getUsername()
							+ "- grade - " + (int) jsGrade.getValue());
		InstructorAssignmentActivities.updateGrade(asgmSub.getAsgmID(), student.getUsername(), (int) jsGrade.getValue());
	}
	
    class FileButton extends JButton{
		
		public File attachedFile;
		
		public FileButton(File file){
			super("File: " + file.getName());
			this.attachedFile = file;
			setForeground(Color.BLUE);
			setOpaque(false);
			setBorder(BorderFactory.createEmptyBorder());
			setCursor(new Cursor(Cursor.HAND_CURSOR));
			addActionListener(new ALDownloadFile(file));
		}
	}
}
